<?php
/**
 * Created by PhpStorm.
 * User: Mauro.Arce
 * Date: 2/11/15
 * Time: 2:56 PM
 */

include 'sql.php';

function specialStringIsUnique($specialStr){
    $db_handle = connect_to_sql();
    $db_found = connect_to_db($db_handle);

    if ($db_found){
        $result = getIdenticalString($specialStr);

        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }

        if (mysql_num_rows($result) > 0){
            mysql_close($db_handle);
            return false;
        }
        else {
            mysql_close($db_handle);
            return true;
        }
    }
    mysql_close($db_handle);
    return false;
}

function insertNewStars($userID, $amountOfNewStars){
    for ($count = 0; $count < $amountOfNewStars; $count++){
        do{
            /*make sure string is unique*/
            $specialStr = substr(str_shuffle(MD5(microtime())), 0, 15);
        } while (!specialStringIsUnique($specialStr));
        insertStarToUser($userID, $specialStr);
    }
}

function distributeNewStars(){
    $db_handle = connect_to_sql();
    $db_found = connect_to_db($db_handle);
    $amountOfNewStars = 2;

    if ($db_found){
        $result = getAllExistingActiveUsers();

        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }
        while ($row = mysql_fetch_array($result)) {
            $userID = $row["userID"];
            insertNewStars($userID, $amountOfNewStars);
        }
    }
}

function populateDropdown(){
    $db_handle = connect_to_sql();
    $db_found = connect_to_db($db_handle);

    if ($db_found){
        $result = getExistingActiveUsers();

        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }

        while ($row = mysql_fetch_array($result)) {
            echo "<option value=\"" . $row["userID"] . "\">" . $row{'username'} . "</option>";
        }

        mysql_close($db_handle);
    }
}

function getNumberOfAvailableStars(){
    $db_handle = connect_to_sql();
    $db_found = connect_to_db($db_handle);
    $starCount = 0;

    if ($db_found){
        $result = getAvailableStarCount();

        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }

        while ($row = mysql_fetch_array($result)) {
            $starCount = $row["StarCount"];
        }
    }
    mysql_close($db_handle);

    return $starCount;
}

function getAvailableStarsInfo(){
    $db_handle = connect_to_sql();
    $db_found = connect_to_db($db_handle);
    $starInfo = Array();
    if ($db_found){
        $result = getAvailableStars();

        if($result === FALSE) {
            die(mysql_error()); // TODO: better error handling
        }

        while ($row = mysql_fetch_array($result)) {
            array_push($starInfo, array($row["userID"], $row["starBagID"], $row["starSpecialGenID"]));
        }
    }
    mysql_close($db_handle);

    return $starInfo;
}

function loadTestimonials(){

}

function loadModal(){
    echo "<div>";
    $maxPossibleStars = getNumberOfAvailableStars();
    echo "<a href=\"#close\" title=\"Close\" class=\"close\">X</a>";
    echo "<h2>Give Stars</h2>";
    echo "<form action=\"send_comment.php\" method=\"POST\">";
    echo "Select User";
    echo "<select name=\"userToSend\">";

    populateDropdown();

    echo "</select>";
    echo "<br>Select # of stars to give (you have $maxPossibleStars):";
    echo "<INPUT TYPE=\"NUMBER\" name=\"starSpinner\" MIN=\"0\" MAX =\"" . $maxPossibleStars . "\" STEP=\"1\" VALUE=\"0\" SIZE=\"6\">";
    echo "<p>Comment:</p><textarea rows=\"4\" cols=\"50\" name=\"comment\"></textarea>";
    echo "<input type=\"submit\">";
    echo "</form>";
    echo "</div>";
}
?>